package com.wmh.baseservice.operationaid.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.operationaid.pojo.vo.task.UpDateTitleReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.task.UpdateTitleOrZtcAddKeyWordReqVO;
import com.wmh.baseservice.operationaid.service.TaskService;
import com.wmh.baseservice.robot.service.RobotInfoService;
import com.wmh.baseservice.robot.tools.MyHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 谢骏
 * @date 2023/6/9 10:03
 **/
@Service
@Slf4j
@Validated
public class TaskServiceImpl implements TaskService {

    @Resource
    private RobotInfoService robotInfoService;

    @Resource
    private MyHttpUtil myHttpUtil;

    @Override
    public void updateTitleOrZtcAddKeyWord(UpdateTitleOrZtcAddKeyWordReqVO reqVO) {
        if (Boolean.TRUE.equals(reqVO.getAddNewTitle())) {
            if (StrUtil.isEmpty(reqVO.getProductId())) {
                throw new ServiceException("请填写修改标题的商品编号");
            }
            if (StrUtil.isEmpty(reqVO.getNewTitle())) {
                throw new ServiceException("请填写新的标题");
            }
        }
        JSONObject params = JSON.parseObject(JSON.toJSONString(reqVO));
        params.put("keywordList", JSON.parseArray(params.getString("keywordList"), String.class));
        robotInfoService.autoPushTask("添加标题与关键词", params);
    }

    @Override
    public void updateTitle(UpDateTitleReqVO reqVO) {
        JSONObject params = new JSONObject();
        params.put("account", reqVO.getAccount());
        params.put("password", reqVO.getPassword());
        List<JSONObject> productTitleList = JSON.parseArray(reqVO.getProductTitleListJson(), JSONObject.class);
        productTitleList.forEach(item -> {
                            item.put("newTitle",item.get("title"));
                            item.put("productId",item.get("id"));
                            item.remove("title");
                            item.remove("id");
        });
        params.put("product", productTitleList);
        robotInfoService.autoPushTask("修改商品标题", params);
    }

}
